/**
 ***********************************************************************
 * Program name: SendMail.java<br>
 * Program description: TODO<br>
 * Author: sugen<br>
 * Revision history:1.0<br>
 * <br>
 * Description of Change	Programmer		Date<br>
 * ---------------------    ----------      ----<br>
 * Initial creation			Su Gen		   2011-3-9 <br>
 * <br>
 * Copyright (c) 2011 by SUGEN ALL RIGHTS RESERVED<br>
 ************************************************************************
 */
package com.gen;

import java.sql.ResultSet;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.common.DBCon;
import com.mail.MailDataBuilder;
import com.mail.EmailSender;

public class SendMail {
	private static Logger log = LogManager.getLogger(SendMail.class);
	boolean result=true;
	public static void main(String[] args) throws Exception {
		EmailSender mailSender= new EmailSender();
		MailDataBuilder bulidMgr=new MailDataBuilder();
		DBCon dbcon= new DBCon();
		ResultSet rs=dbcon.executeQuery("select distinct email from email where sendflag='1'");
		String mailTo="";
		while(rs.next()){
			if(mailTo.equals("")){
				mailTo=rs.getString("email");
			}else{
				mailTo=mailTo+","+rs.getString("email");
			}
		}
		String subject="Customer No Registration";
		String attachment="";
		String context="";
		String mailType="ODC-Monitor-Mail";
		try {
			context = bulidMgr.buildMailData("select customercode as customerCode from customer");
		} catch (Exception e) {
		}
		boolean result=mailSender.send(mailTo, subject, context, attachment);
		logMailResult(result, mailType, mailTo, subject);
	}
	private static void logMailResult(boolean result, String mailType,String mailTo, String subject){
		if (result){
			log.info("Succeed to send "+ mailType + " email to "+ mailTo +". The Subject is "+ subject );
		}else{
			log.error("Failed to send "+ mailType + " email to "+ mailTo +". The Subject is "+ subject );
		}
	}

}
